import { _decorator, Component, Color, Graphics, UITransform } from 'cc';
import HeChengDaXiGua_GamePanel from "./HeChengDaXiGua_GamePanel";
import { HeChengDaXiGua_GameStorage } from "./HeChengDaXiGua_GameStorage";

const { ccclass, property } = _decorator;

@ccclass('HeChengDaXiGuaGameData')
export class HeChengDaXiGua_GameData {
    static W: number = 600;
    static H: number = 600;
    static row: number = 4; // 行数
    static col: number = 4; // 列数
    static size: number = HeChengDaXiGua_GameData.W / HeChengDaXiGua_GameData.row; // 方块的尺寸
    //    // static arrGrids: any[][] = [];
    static arrGrids = [];
    static arrBlocks = [];
    static Game_mode = `mode_3x3`;
    static Score: number = 0;
    static IsMouseDown: boolean = false;
    static 本次滑动是否有合并: boolean = false;
    static colors = [
        { id: 2, color: "#FFF3E7", Label_color: "#424952" },
        { id: 4, color: "#FFEBCE", Label_color: "#424952" },
        { id: 8, color: "#FFC773", Label_color: "#424952" },
        { id: 16, color: "#FFA663", Label_color: "#FFFFFF" },
        { id: 32, color: "#FF8652", Label_color: "#FFFFFF" },
        { id: 64, color: "#FF5D52", Label_color: "#FFFFFF" },
        { id: 128, color: "#C6CB7B", Label_color: "#FFFFFF" },
        { id: 256, color: "#94CF94", Label_color: "#313031" },
        { id: 512, color: "#5ACFAD", Label_color: "#313031" },
        //        // 修改后的部分（每个颜色使用下一级的颜色值）
        { id: 1024, color: "#52B6CF", Label_color: "#313031" },  // ← 原2048的浅蓝
        { id: 2048, color: "#528BCF", Label_color: "#FFFFFF" },   // ← 原4096的中蓝
        { id: 4096, color: "#7B7BCB", Label_color: "#FFFFFF" },   // ← 原8192的紫蓝
        { id: 8192, color: "#9D5ACF", Label_color: "#FFFFFF" },   // ← 原16384的紫
        { id: 16384, color: "#CF5AA7", Label_color: "#FFFFFF" }    // 新生成的紫红色
        
    ];
    static isSlide: boolean = true;
    static direction;
    static gameState;
    static move_duration = 0.2;
    static InitData() {
        
    }
    
    static ClearData() {
        HeChengDaXiGua_GameData.Score = 0;
        // HeChengDaXiGua_GameStorage.data[HeChengDaXiGua_GameData.Game_mode].score = 0;
        // HeChengDaXiGua_GameStorage.data[HeChengDaXiGua_GameData.Game_mode].map = [];
        HeChengDaXiGua_GameStorage.saveData();

        // HeChengDaXiGua_GameData.Score = 0;
        // HeChengDaXiGua_GameData.arrGrids = [];
        // HeChengDaXiGua_GameData.arrBlocks = [];
    }
    static GetScore() {
        return HeChengDaXiGua_GameData.Score;
    }
    static SetScore(_score) {
        HeChengDaXiGua_GameData.Score = _score;
        HeChengDaXiGua_GameStorage.data[HeChengDaXiGua_GameData.Game_mode].score = _score;
        HeChengDaXiGua_GameStorage.saveData();
    }
    static Add_score(_score) {
        let new_score = HeChengDaXiGua_GameData.GetScore() + _score;
        HeChengDaXiGua_GameData.Update_score(new_score);
    }
    static Update_score(_score) {
        HeChengDaXiGua_GameData.SetScore(_score);

        // HeChengDaXiGua_GameData.Score = _score;

        if (HeChengDaXiGua_GameStorage.data[HeChengDaXiGua_GameData.Game_mode].high_score <= HeChengDaXiGua_GameData.Score) {
            HeChengDaXiGua_GameStorage.data[HeChengDaXiGua_GameData.Game_mode].high_score = HeChengDaXiGua_GameData.Score;
            HeChengDaXiGua_GameStorage.saveData();
        }

        HeChengDaXiGua_GamePanel.Instance.Refresh_score();
    }
    static clearFruitsData() {
        if (!HeChengDaXiGua_GameStorage.data) {
            HeChengDaXiGua_GameStorage.initData();
        }

        HeChengDaXiGua_GameStorage.data.fruits = [];
        HeChengDaXiGua_GameStorage.data.current_score = 0;
        HeChengDaXiGua_GameStorage.saveData();
    }
}

export default class DashedLine extends Component {
    @property
    lineLength: number = 20; // 虚线段长度
    @property
    gapLength: number = 10;  // 间隔长度
    @property
    lineWidth: number = 4;   // 线宽
    @property(Color)
    lineColor: Color = Color.WHITE;
    onLoad() {
        const graphics = this.getComponent(Graphics);
        graphics.lineWidth = this.lineWidth;
        graphics.strokeColor = this.lineColor;

        const width = this.node.getComponent(UITransform).width;
        let x = -width / 2;
        const y = 0;

        graphics.moveTo(x, y);
        while (x < width / 2) {
            graphics.lineTo(x + this.lineLength, y);
            graphics.moveTo(x + this.lineLength + this.gapLength, y);
            x += this.lineLength + this.gapLength;
        }
        graphics.stroke();
    }
}

window["HeChengDaXiGua_GameData"] = HeChengDaXiGua_GameData;